import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from chaojiying import Chaojiying_Client
from selenium.webdriver.common.action_chains import ActionChains


class ClickVerificationCode:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.url = 'https://www.geetest.com/Sensebot'
        self.driver.maximize_window()

    def get_image(self):
        self.driver.get(self.url)
        self.driver.execute_script('window.scrollBy(0,1800)')

        #self.driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")

        self.driver.find_element(By.XPATH,
            '//section[contains(@class,"col") and contains(@class,"note-viewer")]/div/div/div[3]/div[2]/div/section/div/div/div/div[2]/div[3]/div[4]').click()

        #self.driver.execute_script("window.scrollTo(0,-20)")
        #self.driver.find_element(By.XPATH,
        #    '//section[contains(@class,"col") and contains(@class,"note-viewer")]/div/div/div[3]/div[2]/div/section/div/div/div[2]/div[2]/form/div[3]').click()
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,
            '//section[contains(@class,"col") and contains(@class,"note-viewer")]/div/div/div[3]/div[2]/div/section/div/div/div[2]/div[2]/form/div[3]')))
        self.driver.find_element(By.XPATH,
            '//section[contains(@class,"col") and contains(@class,"note-viewer")]/div/div/div[3]/div[2]/div/section/div/div/div[2]/div[2]/form/div[3]').click()
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,
                  '//section[contains(@class,"col") and contains(@class,"note-viewer")]/div/div/div[3]/div[2]/div/section/div/div/div[2]/div[2]/form/div[3]/div[2]')))
        self.driver.find_element(By.XPATH,
            '//section[contains(@class,"col") and contains(@class,"note-viewer")]/div/div/div[3]/div[2]/div/section/div/div/div[2]/div[2]/form/div[3]/div[2]').click()

        time.sleep(5)
        # 单击"点选验证码"
        #self.driver.find_element(By.XPATH,
        #    '//div[@class="gt-sensebot"]/div[2]/section[3]/div/div[2]/div[1]/ul/li[3]').click()
        #WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_radar_tip')))
        #self.driver.find_element(By.CLASS_NAME,'geetest_radar_tip').click()
        # 抠图
        # 等待
        WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_window')))
        img_file = self.driver.find_element(By.CLASS_NAME,'geetest_window')
        # 只截取标签 点选图片
        img_file.screenshot('yzm.png')


    def dock_chaojiying(self):
        # 对接超级鹰 填写账号、密码、软件id
        chaojiying = Chaojiying_Client('sinosu', 'Lovejie8cj', '967678')
        im = open('yzm.png', 'rb').read()
        # result存储的是超级鹰以json格式返回的数据
        # {'err_no': 0,    driver.execute_script('window.scrollBy(0,3650)') 'err_str': 'OK', 'pic_id': '1144316386469800081',
        # 'pic_str': '88,175|267,153', 'md5': '8ff4bd04e90b1fe1f5b19076cbcd8d6a'}
        result = chaojiying.PostPic(im, 1902)
        #print(result)
        # 提取文字坐标信息
        coordinates = result.get('pic_str').split('|')
        print(coordinates)
        # 将坐标['x,y','x,y']形式转换成[[x, y], [x, y]]便于Selenium定位
        locations = [[int(number) for number in coordinate.split(',')] for coordinate in coordinates]
        return locations

    def simulated_click(self):
        self.get_image()
        # 等待
        img = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.CLASS_NAME, 'geetest_window')))
        for location in self.dock_chaojiying():
            ActionChains(self.driver).move_to_element_with_offset(
                img, location[0], location[1]).click().perform()
        # 点击确认按钮
        self.driver.find_element(By.CLASS_NAME,'geetest_submit').click()
        # 等待3秒查看是否点击成功
        time.sleep(3)
        self.driver.quit()


if __name__ == '__main__':
    ck = ClickVerificationCode()
    ck.simulated_click()
